package cc.linker.steplink.crm.merchant.controller;

import cc.linker.steplink.crm.commons.model.PcUserInfo;
import cc.linker.steplink.crm.merchanth5.commons.UserInfoUtil;
import cc.linker.steplink.crm.merchant.service.WxService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/info")
public class InfoController {

    @Autowired
    private WxService wxService;
    @Autowired
    private UserInfoUtil userInfoUtil;

    @GetMapping("/getToken")
    public Object getToken(HttpServletRequest request) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        Map<String, Object> map = new HashMap<>();
        String token = wxService.getToken(orgId);
        map.put("token", token);
        return map;
    }

    /**
     * 概况
     */
    @GetMapping("/overview")
    public Map<String, Object> overview(HttpServletRequest request) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        String adminId = userinfo.getAdminId();
        Boolean ifSales = userinfo.getSaler();
        return wxService.overview(orgId, adminId, ifSales);
    }

    /**
     * 智能助手
     */
    @GetMapping("/smart")
    public Map<String, Object> smart(HttpServletRequest request) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        String adminId = userinfo.getAdminId();
        Boolean ifSales = userinfo.getSaler();
        return wxService.smart(orgId, adminId, ifSales);
    }

    /**
     * 动态
     */
    @GetMapping("/dynamic")
    public PageInfo dynamic(HttpServletRequest request, Integer pageIndex, Integer pageSize) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        return wxService.dynamic(orgId, pageIndex, pageSize);
    }

    /**
     * 删除快捷入口 entrydel
     *
     * @param entryids 快捷入口ids[数组]
     */
    @PostMapping("/entrydel")
    public void entrydel(@RequestBody Map<String, Object> params, HttpServletRequest request) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        String adminId = userinfo.getAdminId();
        params.put("uid", adminId);
        params.put("orgcode", orgId);
        wxService.entrydel(params);
    }

    /**
     * 获取所有的菜单 allmenu
     */
    @PostMapping("/allmenu")
    public Object allmenu(HttpServletRequest request) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        Map<String, Object> params = new HashMap<>();
        String orgId = userinfo.getOrgId();
        String adminId = userinfo.getAdminId();
        params.put("uid", adminId);
        params.put("orgcode", orgId);
        return wxService.allmenu(params);
    }

    /**
     * 获取用户设置的快捷入口 menuentry
     */
    @PostMapping("/menuentry")
    public Object menuentry(HttpServletRequest request) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        Map<String, Object> params = new HashMap<>();
        String orgId = userinfo.getOrgId();
        String adminId = userinfo.getAdminId();
        params.put("uid", adminId);
        params.put("orgcode", orgId);
        return wxService.menuentry(params);
    }

    /**
     * 设置快捷入口 entryset
     *
     * @param menuids 快捷入口ids[数组]
     */
    @PostMapping("/entryset")
    public void entryset(@RequestBody Map<String, Object> params, HttpServletRequest request) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        String adminId = userinfo.getAdminId();
        params.put("uid", adminId);
        params.put("orgcode", orgId);
        wxService.entryset(params);
    }
}
